{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d7b0445",
   "metadata": {},
   "source": [
    "# 含液体层的反射透射系数矩阵详细表达式\n",
    "\n",
    "+ Author: Zhu Dengda  \n",
    "+ Email:  zhudengda@mail.iggcas.ac.cn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "edd8016c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.set_printoptions(linewidth=200)\n",
    "\n",
    "import sympy as sp\n",
    "from sympy.printing.latex import latex\n",
    "from IPython.display import display, Math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad1fbea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义基本变量\n",
    "k, b1, a1, mu1, Omg1, kb1, ka1, w = sp.symbols(r'k b_1 a_1 mu_1 \\Omega_1 k_{b1} k_{a1} \\omega')\n",
    "b2, a2, mu2, Omg2, kb2, ka2 = sp.symbols(r'b_2 a_2 mu_2 \\Omega_2 k_{b2} k_{a2}')\n",
    "lam1, lam2 = sp.symbols(r'\\lambda_1 \\lambda_2')\n",
    "rho1, rho2 = sp.symbols(r'\\rho_1 \\rho_2')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2326dc47",
   "metadata": {},
   "source": [
    "## 动态解"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aa83ae5",
   "metadata": {},
   "source": [
    "### 液体-液体界面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "178897dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{- \\rho_{1} a_{2} + \\rho_{2} a_{1}}{\\rho_{1} a_{2} + \\rho_{2} a_{1}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{\\rho_{1} a_{2} - \\rho_{2} a_{1}}{\\rho_{1} a_{2} + \\rho_{2} a_{1}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{2 \\rho_{1} a_{1}}{\\rho_{1} a_{2} + \\rho_{2} a_{1}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{2 \\rho_{2} a_{2}}{\\rho_{1} a_{2} + \\rho_{2} a_{1}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# left\n",
    "Pl = sp.Matrix([\n",
    "    [-a1, -a2],\n",
    "    [rho1*w**2, -rho2*w**2]\n",
    "])\n",
    "\n",
    "# right\n",
    "Pr = sp.Matrix([\n",
    "    [-a2, -a1],\n",
    "    [rho2*w**2, -rho1*w**2]\n",
    "])\n",
    "\n",
    "Q = sp.together(sp.expand(Pl.inv() * Pr))\n",
    "\n",
    "TU = Q[:1, :1]\n",
    "RU = Q[1:, :1]\n",
    "RD = Q[:1, 1:]\n",
    "TD = Q[1:, 1:]\n",
    "\n",
    "display(Math(latex(RD)))\n",
    "display(Math(latex(RU)))\n",
    "display(Math(latex(TD)))\n",
    "display(Math(latex(TU)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1414e2e",
   "metadata": {},
   "source": [
    "### 液体-固体界面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ae6594d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{2 \\Omega_{2}^{2} a_{1} \\mu_{2} + \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} - \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{- 2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}} & - \\frac{4 \\Omega_{2} a_{1} b_{2} k \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\\\- \\frac{4 \\Omega_{2} a_{1} a_{2} k \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}} & \\frac{- 2 \\Omega_{2}^{2} a_{1} \\mu_{2} + \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} - \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- \\frac{2 \\Omega_{2} \\omega^{2} \\rho_{1} a_{1}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\\\- \\frac{2 \\omega^{2} \\rho_{1} a_{1} a_{2} k}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{4 \\Omega_{2} a_{2} \\mu_{2} \\left(\\Omega_{2} - k^{2}\\right)}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}} & \\frac{4 a_{2} b_{2} k \\mu_{2} \\left(\\Omega_{2} - k^{2}\\right)}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# left\n",
    "Pl = sp.Matrix([\n",
    "    [(-1)*a1,           -a2,             k,           ],\n",
    "    [(-1)*-rho1*w**2 ,  2*mu2*Omg2,     -2*k*mu2*b2,  ],\n",
    "    [0,                -2*k*mu2*a2,      2*mu2*Omg2,  ]\n",
    "])\n",
    "\n",
    "# right\n",
    "Pr = sp.Matrix([\n",
    "    [(-1)*a2,            (-1)*k,            -a1        ],\n",
    "    [(-1)*2*mu2*Omg2,    (-1)*2*k*mu2*b2,   -rho1*w**2 ],\n",
    "    [(-1)*2*k*mu2*a2,    (-1)*2*mu2*Omg2,   0          ]   \n",
    "])\n",
    "\n",
    "Q = sp.together(sp.expand(Pl.inv() * Pr))\n",
    "\n",
    "TU = Q[:1, :2]\n",
    "RU = Q[1:, :2]\n",
    "RD = Q[:1, 2:]\n",
    "TD = Q[1:, 2:]\n",
    "\n",
    "display(Math(latex(RD)))\n",
    "display(Math(latex(RU)))\n",
    "display(Math(latex(TD)))\n",
    "display(Math(latex(TU)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34c7f2cf",
   "metadata": {},
   "source": [
    "### 固体-液体界面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e6b9f9e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{- 2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}} & \\frac{4 \\Omega_{2} a_{1} b_{2} k \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\\\\\frac{4 \\Omega_{2} a_{1} a_{2} k \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}} & \\frac{- 2 \\Omega_{2}^{2} a_{1} \\mu_{2} + \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} - \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{2 \\Omega_{2}^{2} a_{1} \\mu_{2} + \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} - \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{4 \\Omega_{2} a_{2} \\mu_{2} \\left(\\Omega_{2} - k^{2}\\right)}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}} & \\frac{4 a_{2} b_{2} k \\mu_{2} \\left(- \\Omega_{2} + k^{2}\\right)}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- \\frac{2 \\Omega_{2} \\omega^{2} \\rho_{1} a_{1}}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\\\\\frac{2 \\omega^{2} \\rho_{1} a_{1} a_{2} k}{2 \\Omega_{2}^{2} a_{1} \\mu_{2} - \\Omega_{2} \\omega^{2} \\rho_{1} a_{2} + \\omega^{2} \\rho_{1} a_{2} k^{2} - 2 a_{1} a_{2} b_{2} k^{2} \\mu_{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# left\n",
    "Pl = sp.Matrix([\n",
    "    [a1,           a2,           k,  ],\n",
    "    [rho1*w**2 ,   2*mu2*Omg2,     2*k*mu2*b2,  ],\n",
    "    [0,            2*k*mu2*a2,    2*mu2*Omg2,  ]\n",
    "])\n",
    "\n",
    "# right\n",
    "Pr = sp.Matrix([\n",
    "    [(-1)*-a2,            (-1)*k,            a1],\n",
    "    [(-1)*2*mu2*Omg2,     (-1)*-2*k*mu2*b2,  -rho1*w**2 ],\n",
    "    [(-1)*-2*k*mu2*a2,    (-1)*2*mu2*Omg2,  0]   \n",
    "])\n",
    "\n",
    "Q = sp.together(sp.expand(Pl.inv() * Pr))\n",
    "\n",
    "# 注意索引和之前不同\n",
    "TD = Q[:1, :2]\n",
    "RD = Q[1:, :2]\n",
    "RU = Q[:1, 2:]\n",
    "TU = Q[1:, 2:]\n",
    "\n",
    "# 最终表达式和液体-固体界面的情况很类似，\n",
    "# 仅替换 TD <-> TU, RD <-> RU ，以及部分非对角线上的符号\n",
    "# 这在编程上提供了便利\n",
    "display(Math(latex(RD)))\n",
    "display(Math(latex(RU)))\n",
    "display(Math(latex(TD)))\n",
    "display(Math(latex(TU)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
